<template>
  <div class="mod-user">
    <el-button plain @click="Withdrawal">提现管理</el-button>
    <el-button plain disabled>提现记录</el-button>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <div class="seacrhMoney">
        <el-form-item>
          <div class="balance-searchMoney">
            提现申请单号
            <el-input class="input" v-model="dataForm.userName" placeholder="请输入提现申请单号"></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            提现申请时间
            <el-date-picker v-model="value" type="daterange" value-format="yyyy-MM-dd HH:mm:ss" class="input" start-placeholder="开始日期" :clearable='false' end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            提现申请单位
            <el-select v-model="value1" filterable placeholder="请选择">
              <el-option v-for="item in city" :key="item.legalAccountCode" :label="item.legalCompanyName" :value="item.legalAccountCode">
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            打款时间
            <el-date-picker v-model="value4" type="daterange" value-format="yyyy-MM-dd HH:mm:ss" class="input" start-placeholder="开始日期" :clearable='false' end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <!-- <el-form-item>
          <div class="balance-searchMoney">
            提现状态
            <el-select v-model="status" @change="stWithdrawal" clearable placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-form-item> -->
      </div>
      <el-form-item>
        <el-button type="primary" @click="demand">查询</el-button>
        <el-button type="primary" @click="empty">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <div class="export">
      <div style="display: flex; justify-content: flex-end;margin-bottom:10px;">
        <el-button type="primary" @click="excel">导出Excel</el-button>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" element-loading-text="数据加载中" element-loading-spinner="el-icon-loading" element-loading-custom-class="loading_color" style="width: 100%;">
        <template slot="empty">
          <img class="machine" src="../../assets/img/machine.svg" alt="" />
          <div style="font-size: 14px; text-align: center">暂无数据</div>
        </template>
        <el-table-column prop="applyNo" header-align="center" align="center" label="提现申请单号" width="150">
        </el-table-column>
        <el-table-column prop="applyTime" header-align="center" align="center" label="提现申请时间" width="150">
        </el-table-column>
        <el-table-column prop="cycleNos" header-align="center" align="center" label="提现申请单位" width="150">
          <template slot-scope="scope">
            <div>
              {{ scope.row.legalCompanyName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="accountNickname" header-align="center" align="center" label="提现申请人" width="100">
        </el-table-column>
        <el-table-column prop="applyFee" header-align="center" align="center" label="提现金额(元)" width="100">
          <template slot-scope="scope">
            <div>
              {{ scope.row.applyFee| MoneyFormat }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="withdrawFee" header-align="center" align="center" label="提现手续费(元)" width="120">
          <template slot-scope="scope">
            <div>{{ scope.row.withdrawFee| MoneyFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="actualFee" header-align="center" align="center" label="打款金额">
          <template slot-scope="scope">
            <div class="">{{ scope.row.actualFee| MoneyFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="cycleNos" header-align="center" align="center" label="关联结算单号" width="150" :render-header="renderHeader">
          <template slot-scope="scope">
            <div v-for="(title, index) in scope.row.cycleNos" :key="index" class="cycleNos">
              <div @click="addOrUpdateHandles(title)">{{ title }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="auditTime" header-align="center" align="center" label="提现审批时间" width="150">
        </el-table-column>
        <el-table-column prop="approverCompanyName" header-align="center" align="center" label="审批单位">
        </el-table-column>
        <el-table-column prop="approverFinanceName" header-align="center" align="center" label="审批人">
        </el-table-column>
        <el-table-column prop="applyStatus" header-align="center" align="center" width="150" label="提现状态">
          <template slot-scope="scope">
            <div v-if="scope.row.applyStatus === 0" class="orangeMoney">
              提现审批中
            </div>
            <div v-if="scope.row.applyStatus === 1" class="greenMoney">
              完成提现
            </div>
            <div v-if="scope.row.applyStatus === 2" class="grayMoney">
              <el-tooltip class="item" effect="dark" :content="scope.row.rejectReason" placement="top-start">
                <div class="newTitle">
                  <div class="grayMoneys">
                    提现审批未通过
                  </div>
                  <i name="wenhao" class="el-icon-question wenhao"></i>
                </div>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="orderNo" header-align="center" align="center" label="打款单号" width="150">
        </el-table-column>
        <el-table-column prop="agentStatus" header-align="center" align="center" label="打款状态" width="150">
          <template slot-scope="scope">
            <div size="small" type="danger">
              {{scope.row.agentStatus == null?'':scope.row.agentStatus == '00'?'打款成功':scope.row.agentStatus == '03'?'打款失败':''}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="agentTime" header-align="center" align="center" label="打款时间" width="150">
        </el-table-column>
        <el-table-column prop="bankAccountName" header-align="center" align="center" label="银行名称">
        </el-table-column>
        <el-table-column prop="bankCardCode" header-align="center" align="center" label="银行账号" width="150">
        </el-table-column>
      </el-table>
    </div>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import Vue from 'vue'
import { formatDate } from '../../utils/time.js'
export default {
  data () {
    return {
      dataForm: {
        userName: ''
      },
      len: '',
      applyNo: '',
      userCode: '',
      applyTimeStart: '',
      applyTimeEnd: '',
      legalAccountCode: '',
      agentTimeStart: '',
      agentTimeEnd: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      applyStatus: '',
      status: '',
      value1: '',
      value: '',
      value2: '',
      value4: '',
      city: [],
      options: [
        {
          value: "0",
          label: "提现审核中",
        },
        {
          value: "1",
          label: "提现审核通过",
        },
        {
          value: "2",
          label: "提现审核不通过",
        },

      ],
    }
  },
  activated () {
    this.cycleNo = this.$route.query.cycleNo
    this.applyNo = this.$route.query.applyNo
    this.userCode = localStorage.getItem('userCode')
    console.log(this.applyNo)
    if (this.cycleNo) {
      this.init()
    } else {
      this.getDataList()
    }
    if (!localStorage.getItem('order_hover_num')) {
      localStorage.setItem('order_hover_num', 1)
      window.location.reload()
    }
    this.cityList()
  },
  methods: {
    // 表格头部提示事件
    renderHeader (h, { column }) {
      return h('span', [
        h('span', column.label),
        h(
          'el-tooltip',
          {
            props: {
              effect: 'dark',
              content: '本次提现，申请的是哪条结算账单',
              placement: 'top'
            }
          },
          [
            h('i', {
              class: 'el-icon-question',
              style: 'color:#000;margin-left:5px;'
            })
          ],
          {
            content: '本次提现，申请的是哪条结算账单'
          }
        )
      ])
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/balanceDetail/getCityWithdrawDetailList'
        ),
        method: 'get',
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.applyNo,
          applyTimeStart: this.applyTimeStart,
          applyTimeEnd: this.applyTimeEnd,
          legalAccountCode: this.legalAccountCode,
          agentTimeStart: this.agentTimeStart,
          agentTimeEnd: this.agentTimeEnd,
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.applyStatus
        })
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list
          this.totalPage = data.data.total
          console.log(data.data)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 提现状态
    // stWithdrawal (e) {
    //   this.applyStatus = e;
    //   this.pageIndex = 1;
    //   this.getDataList();
    // },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/balanceDetail/getCityWithdrawDetailList'
        ),
        method: 'get',
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.value1,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.applyStatus
        })
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list
          this.totalPage = data.data.total
          console.log(data.data)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/balanceDetail/getCityWithdrawDetailList'
        ),
        method: 'get',
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.value1,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.applyStatus
        })
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list
          this.totalPage = data.data.total
          console.log(data.data)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //导出
    excel () {
      if (this.value == '') {
        var valuestart = this.value
        var valueend = this.value
      } else {
        var valuestart = this.value[0]
        var valueend = this.value[1]
      }
      if (this.value4 == '') {
        var value4start = this.value4
        var value4end = this.value4
      } else {
        var value4start = this.value4[0]
        var value4end = this.value4[1]
      }
      window.location.href =
        `https://api.xstarlife.com/cassadmin/balanceDetail/downloadAllCityWithdrawDetail?userType=2&pageNum=1&pageSize=100&agentTimeStart=${value4start}&agentTimeEnd=${value4end}&applyStatus=${this.applyStatus}&legalAccountCode=${this.value1}&applyNo=${this.dataForm.userName}&applyTimeStart=${valuestart}&applyTimeEnd=${valueend}&userCode=${this.userCode}`
    },
    // 查看单号
    addOrUpdateHandles (cycleNo) {
      this.$router.push({
        name: `totalFinance-settlementList`,
        query: {
          cycleNo: cycleNo
        }
      })
    },
    //查询
    demand () {
      this.pageIndex = 1
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/balanceDetail/getCityWithdrawDetailList'
        ),
        method: 'get',
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.value1,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.applyStatus
        })
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list
          this.len = data.data.list.length
          this.totalPage = data.data.total
          console.log(data.data)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //清空
    empty () {
      this.dataForm.userName = ''
      this.value = ''
      this.value1 = ''
      this.value4 = ''
      this.getDataList()
    },
    // 获取数据列表
    cityList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/legalCompany/getByCurrentFinanceCode'
        ),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data.success == 1) {
          this.city = data.data
        } else {
          this.$message.error(data.errorMsg)
        }
        this.dataListLoading = false
      })
    },
    // 提现管理
    Withdrawal () {
      this.$router.push({
        name: `totalFinance-Withdrawal`
      })
    },
    // 全部单号
    addMoney (cycleNos) {
      this.$router.push({
        name: `totalFinance-settlementDetail`,
        query: {
          cycleNos: cycleNos
        }
      })
    },
    init () {
      this.$http({
        url: this.$http.adornUrl(
          'cassadmin/balanceDetail/getCityWithdrawDetail'
        ),
        method: 'get',
        params: this.$http.adornParams({
          cycleNo: this.cycleNo
        })
      }).then(({ data }) => {
        if (data.success == 1) {
          console.log(data)
          this.dataList.push(data.data)
          this.totalPage = 1
          if (this.dataList.applyTime) {
            this.dataList.applyTime = formatDate(
              this.dataList.applyTime,
              'yyyy-MM-dd hh:mm'
            )
          }
          if (this.dataList.auditTime) {
            this.dataList.auditTime = formatDate(
              this.dataList.auditTime,
              'yyyy-MM-dd hh:mm'
            )
          }
          if (this.dataList.agentTime) {
            this.dataList.agentTime = formatDate(
              this.dataList.agentTime,
              'yyyy-MM-dd hh:mm'
            )
          }
        } else {
          this.$message.error(data.errorMsg)
        }
      })

      this.visible = true
    }
  },
  deactivated () {
    localStorage.removeItem('order_hover_num')
  }
}
</script>
<style scoped>
.input {
  flex: 1;
  margin-left: 5px;
}

.balance-searchMoney {
  display: flex;
  align-items: center;
}
.export {
  margin-top: 30px;
  background-color: #fff;
  padding: 20px;
  border-radius: 6px;
}
.grayMoneys {
  margin-right: 5px;
}
.wenhao {
  color: #000;
}
</style>
